<?php

// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      

//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2022/05/20
//      |_|                                         | TpMeCMS

declare(strict_types=1);

namespace ImiApp\ApiServer\Api\Controller\user;

use Imi\Db\Db;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use Phpben\Imi\Validate\Annotation\Validate;
use ImiApp\ApiServer\Common\AuthApi;
use ImiApp\ApiServer\Api\Controller\Tpmecms;

/**
 * 拍卖计算
 * 
 */
class AuctionOrderBean extends AuctionBean
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    protected $user_id;
    protected $auction_id;
    protected $price;

    protected $store_id;

    protected $user;
    protected $auction;
    protected $store;
    protected $goods;
    protected $goods_sku_price_data;
    public $auction_success;
    public $addr;

    //地址id
    protected $addr_id;

    //运费
    protected $_运费 = 0;

    public $使用贝壳 = 0;
    public $保价买 = false;

//    protected $已出价贝壳 = 0;
//    protected $已出价_改价积分 = 0;
//
//    protected $实付现金 = 0;
//    protected $实付贝壳 = 0;
//
//    protected $出局贝壳补贴之和 = 0;
//    protected $报名出局贝壳补贴之和 = 0;
//
//    protected $报名出局积分补贴之和 = 0;
//    protected $出价贝壳之和 = 0;

    public $store_coupons_id = 0;
    public $sys_coupons_id = 0;
    public $auction_server_coupons_id = 0;

    protected $店铺优惠券 = false;
    protected $平台优惠券 = false;
    protected $平台拍卖服务费优惠券 = false;
    

    protected $推荐店铺优惠券 = false;
    protected $推荐平台优惠券 = false;

    protected $店铺优惠金额 = 0;
    protected $平台优惠金额 = 0;
    protected $平台拍卖服务费优惠金额 = 0;

    protected $商品邮寄还是服务 = 1;//1=拍卖技术费%(邮寄,2=拍卖技术费%(服务


    protected $str = '';
    protected $array = [];

    public function 合并($str = null)
    {
        // if ($str) {
        //     $this->str = $this->str . '👉' . $str;
        // }
        // // var_dump($this->str);
        if ($str) {
            if (!in_array($str, $this->array)) {
                $this->array[] = $str;
            }
        }
    }

    /**
     * 设置拍中id
     * @param int $success_id 成功id
     * @return void
     */
   public function setSuccessId($success_id){


        $this->auction_success = Db::name('auction_success')->where('id','=',$success_id)->find();
       var_dump($success_id);
        if(!$this->auction_success){
//            return $this->error('没有该拍中的拍卖');
            return '没有该拍中的拍卖';
        }

        $this->setAuctionId($this->auction_success['auction_id']);
        $this->setUserId($this->auction_success['user_id']);
    }


    public function setUserId($user_id){
        $this->user_id = $user_id;
        $this->user = Db::name('user')->where('id','=',$this->user_id)->find();
    }

    public function setAuctionId($auction_id){
        $this->auction_id = $auction_id;
        $this->auction = Db::name('auction')->where('id','=',$this->auction_id)->find();

        //拍卖类型:1=贝壳竞拍,2=降价拍,3=升价拍,4=暗拍(降价拍),5=暗拍(升价拍)
        switch ($this->auction['type_status']) {
            case '1':
                $this->setPrice($this->auction_success['shell']);
                break;
            
            default:
                $this->setPrice($this->auction_success['money']);
                break;
        }

        $this->store_id = $this->auction['store_id'];
        $this->store = Db::name('store')->where('id','=',$this->store_id)->fieldRaw('id,name,logo_image,set_id,run_id')->find();
        $this->goods = Db::name('goods')->where('id','=',$this->auction['goods_id'])->fieldRaw('id,title,image,views,send_addr,new_status')->find();

        $this->goods_sku_price_data = Db::name('goodssku_price')->where('id','=',$this->auction_success['goods_sku_price_id'])->fieldRaw('id,goods_sku_text,image,stock,goods_sku_ids,price')->find();

        //商品类型:normal=实体商品,virtual=虚拟商品,service=服务
        switch ($this->goods['type']) {
            case 'normal':
                $this->商品邮寄还是服务 = 1;//1=拍卖技术费%(邮寄,2=拍卖技术费%(服务
                break;
            
            default:
                $this->商品邮寄还是服务 = 2;
                break;
        }

    }

    public function setPrice($price){
        $this->price = $price;
    }

    public function setAddrId(?int $addr_id){
        $this->addr_id = $addr_id;
        $this->addr = $this->GetAddrData($this->addr_id);
    }



    public function getGoodsInfo(){
        return $this->goods;
    }

    public function getStoreInfo(){
        return $this->store;
    }

    public function getGoodsSkuInfo(){
        return $this->goods_sku_price_data;
    }


    

    


    public function 使用贝壳(?float $shell){
           
        if($this->price < $shell){
            throw new \Exception("使用贝壳大于价格", -1);
        }
        $this->使用贝壳 = $shell;
    }



    public function 使用店铺优惠券(?int $coupons_id){
       //判断用户是否有该优惠券
        $店铺优惠券 = $this->getStoreCouponIsUse($coupons_id, $this->使用贝壳后的价格(), $this->user_id, $this->store_id);
        if($店铺优惠券){
            $this->store_coupons_id = $coupons_id;
            $this->店铺优惠券 = $店铺优惠券;
            $this->店铺优惠金额 = $店铺优惠券['jian'];
        }
    }


    public function 使用平台优惠券(?int $coupons_id){
        //判断用户是否有该优惠券
         $平台优惠券 = $this->getCouponIsUse($coupons_id, $this->使用贝壳后的价格() - $this->店铺优惠金额, $this->user_id);
         if($平台优惠券){
             $this->sys_coupons_id = $coupons_id;
             $this->平台优惠券 = $平台优惠券;
             $this->平台优惠金额 = $平台优惠券['jian'];
         }
    }

    public function 使用平台拍卖服务费优惠券(?int $coupons_id){
        //判断用户是否有该优惠券
         $平台拍卖服务费优惠券 = $this->getSysCouponAuctionServerIsUse($coupons_id, $this->买家拍卖服务费(), $this->user_id);
         if($平台拍卖服务费优惠券){
             $this->auction_server_coupons_id = $coupons_id;
             $this->平台拍卖服务费优惠券 = $平台拍卖服务费优惠券;
             $this->平台拍卖服务费优惠金额 = $平台拍卖服务费优惠券['jian'];
         }
    }

    public function  使用贝壳后的价格(){
        if(($this->price - $this->使用贝壳)<0){
            throw new \Exception("使用贝壳大于价格", -1);
        }
       return  $this->price - $this->使用贝壳;
    }




    /**
     * 计算
     * @return void
     */
    public function count(){
        $买家拍卖服务费 = $this->买家拍卖服务费();
        $卖家拍卖服务费 = $this->卖家拍卖服务费();

        if($this->addr_id>0){
            //获取运费
            $this->_运费 = $this->GetDispatchAmount($this->auction->dispatch_ids,$this->addr_id);
        }



        $实付现金 = $this->实付现金();
        $实付贝壳 = $this->实付贝壳();

        $data['user_auction_server'] = $买家拍卖服务费;
//        $data['卖家拍卖服务费'] = $卖家拍卖服务费;
        $data['pay_money'] = $实付现金;
        $data['pay_shell'] = $实付贝壳;
        $data['dispatch_amount'] = $this->_运费;
        $data['sys_coupons_id'] = $this->sys_coupons_id;
        $data['store_coupons_id'] = $this->store_coupons_id;
        $data['auction_server_coupons_id'] = $this->auction_server_coupons_id;
        
        $data['sys_coupons'] = $this->平台优惠券;
        $data['store_coupons'] = $this->店铺优惠券;
        $data['auction_server_coupons'] = $this->平台拍卖服务费优惠券;

 

        switch ($this->auction['type_status']) {
            case '1':
                $拍卖类型 = '贝壳竞拍';
   

                break;
            case '2':
                $拍卖类型 = '降价拍';

                break;
            case '3':
                $拍卖类型 = '升价拍';
                break;
            case '4':
                $拍卖类型 = '暗拍(降价拍)';
                $data['auction_offer_shell_count'] = $this->拍卖改价总贝壳($this->auction_id);
                break;
            case '5':
                $拍卖类型 = '暗拍(升价拍)';
                $data['auction_offer_shell_count'] = $this->拍卖改价总贝壳($this->auction_id);
                break;
            default:
                # code...
                break;
        }
        $data['auction_enroll_score_count'] = $this->拍卖报名总积分($this->auction_id);
        $data['auction_enroll_shell_count'] = $this->拍卖报名总贝壳($this->auction_id);
        
        $data['auction_type'] = $拍卖类型;
        
        return $data;
    }

    protected function 买家拍卖服务费比率(){     
        $买家拍卖服务费比率 = $this->GetSetserveruserVal($this->user_id, $this->user['set_id'], 4); //服务比例id1=支付现金贝壳返现%,2=积分最多可用%,3=积分服务费%,4=拍卖服务费%,5=立即买单 限额/日(元),6=提现服务费%,7=贝壳转移服务费%
        $买家拍卖服务费比率 = $this->ChangeRatio($买家拍卖服务费比率);
        $this->合并('买家拍卖服务费比率'.$买家拍卖服务费比率);
        return $买家拍卖服务费比率;
    }


    protected function 买家拍卖服务费(){
        $买家拍卖服务费比率 = $this->买家拍卖服务费比率(); //服务比例id1=支付现金贝壳返现%,2=积分最多可用%,3=积分服务费%,4=拍卖服务费%,5=立即买单 限额/日(元),6=提现服务费%,7=贝壳转移服务费%
        var_dump('买家拍卖服务费比率'.$买家拍卖服务费比率);
        $买家拍卖服务费 = $买家拍卖服务费比率 * $this->price - $this->平台拍卖服务费优惠金额;
        if($买家拍卖服务费>0){

        }else{
            $买家拍卖服务费 = 0;
        }
        $this->合并('买家拍卖服务费'.$买家拍卖服务费);
        return $买家拍卖服务费;
    }


    protected function 卖家拍卖服务费(){
        //_AC
        $卖家拍卖服务费比率 = $this->卖家拍卖服务费比率(); //服务比例id1=支付现金贝壳返现%,2=积分最多可用%,3=积分服务费%,4=拍卖服务费%,5=立即卖单 限额/日(元),6=提现服务费%,7=贝壳转移服务费%
        $卖家拍卖服务费 = ($this->拍品现金() + $this->保价买现金() - $this->售后退实付现金() ) * $卖家拍卖服务费比率;
        // $卖家拍卖服务费 = $卖家拍卖服务费比率 * $this->price - $this->平台优惠金额;
        // if($卖家拍卖服务费>0){

        // }else{
        //     $卖家拍卖服务费 = 0;
        // }
        // $this->合并('卖家拍卖服务费'.$卖家拍卖服务费);
         return $卖家拍卖服务费;
    }

    protected function 售后退实付现金(){
        //_AU
        return 0;
    }


    protected function 卖家拍卖服务费比率(){
        //_AB
        $this->合并('商品邮寄还是服务'.$this->商品邮寄还是服务.($this->商品邮寄还是服务==1?'拍卖技术费%(邮寄':'拍卖技术费%(服务'));
        $卖家拍卖服务费比率 = $this->GetSetserverstoreVal($this->store_id, $this->store['set_id'], $this->商品邮寄还是服务); //1=拍卖技术费%(邮寄,2=拍卖技术费%(服务,3=拼团技术费%(邮寄,4=拼团技术费%(服务,5=贝壳服务费%,6=积分服务费%,7=调货费%,8=现金服务费%(邮寄,9=现金服务费%(服务,10=立即买单%,11=保证金阀值(元),12=借米额度(M倍),13=贝壳转移服务费%,14=提现手续费%
        $卖家拍卖服务费比率 = $this->ChangeRatio($卖家拍卖服务费比率);
        $this->合并('卖家拍卖服务费比率'.$卖家拍卖服务费比率);
        return $卖家拍卖服务费比率;
    }


    public function 保价买现金(){
        //_F
        $res = 0;
        //在保价买里面才计算
        if($this->保价买){
             $res = $this->price - $this->使用贝壳 - $this->店铺优惠金额 - $this->平台优惠金额 - $this->已出价贝壳() - $this->已出价_改价积分();
             if($res<0){
                 $this->合并("保价买现金！！！小于0");
                 var_dump("保价买现金！！！小于0");
             }
        }
        return $res;
    }


    public function 拍品现金(){
        //_E
        $res = 0;
        //在非保价买里面才计算
        if(!$this->保价买){
            $res = $this->price - $this->使用贝壳 - $this->店铺优惠金额 - $this->平台优惠金额 - $this->已出价贝壳() - $this->已出价_改价积分();
            if($res<0){
                $this->合并("拍品现金！！！小于0");
                var_dump("拍品现金！！！小于0");
            }
        }
        return $res;
    }

    public function 券后贝壳(){
        //_D
        $shell = 0;
    // 拍卖类型:1=贝壳竞拍,2=降价拍,3=升价拍,4=暗拍(降价拍),5=暗拍(升价拍)
        switch ($this->auction['type_status']){
            case '3':
                return $this->使用贝壳;
                break;

            default:
                return $this->实付贝壳();
//                return $this->使用贝壳 - $this->退款贝壳();
                break;
        }
        return $shell;
    }



    public function 退款贝壳()
    {
        //_AV
        return 0;
    }

    public function 已出价贝壳(){
        //_O
        $res = 0;
        //在保价买里面才计算已出价贝壳
        if($this->保价买) {
            $shell = Db::name('auction_offer')->where('auction_id', '=', $this->auction_id)->where('user_id', '=', $this->user_id)->sum('shell');
            if ($shell > 0)
                return $shell;
        }
        return $res;
    }

    public function 已出价_改价积分(){
        //_P
        $res = 0;
        //在保价买里面才计算
        if($this->保价买) {
            $出价积分 = Db::name('auction_offer')->where('auction_id', '=', $this->auction_id)->where('user_id', '=', $this->user_id)->sum('score');
            if ($出价积分 > 0)
                return $出价积分;
        }
        return $res;
    }


    public function 实付现金(){
        //_Q
        $res = 0;
        //可能要修改
        $res = $this->拍品现金() + $this->保价买现金() + $this->买家拍卖服务费() + $this->_运费;

        return $res;
    }

    public function 实付贝壳(){
        //_R

        $res = $this->使用贝壳 - $this->退款贝壳();
        if($res<0){
            $this->合并('实付贝壳小于0！！！！'.$res);
        }
        return $res;
    }


    public function 出局贝壳补贴之和(){
        $res = 0;
        return $res;
    }

    public function 报名出局贝壳补贴之和(){
        $res = 0;
        return $res;
    }


    public function 报名出局积分补贴之和(){
        $res = 0;
        return $res;
    }

    public function 出价贝壳之和(){
        $res = 0;
        return $res;
    }

    

    


    // /**
    //  * 店铺认证支付成功
    //  *
    //  * @param array $auction 拍卖
    //  * @param int $goods_sku_price_id
    //  * @return void
    //  */
    // protected function 验证拍卖是否有这个规格($auction,  $goods_sku_price_id)
    // {
    //     $goods_sku_price_ids = explode(',',$auction['goods_sku_price_ids']);
    //     if(!in_array($goods_sku_price_id,$goods_sku_price_ids)){
    //         return '该拍卖不存在该商品库存'.$goods_sku_price_id;
    //     }
    //     return 'ok';
    // }
}
